package com.hulk.dryad.activiti7.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hulk.dryad.activiti7.api.vo.WorkflowLeaveVO;
import com.hulk.dryad.activiti7.mapstruct.WorkflowLeaveStruct;
import com.hulk.dryad.common.annotation.OperLog;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.annotation.RespExcel;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.constant.enums.ActionType;
import com.hulk.dryad.common.constant.enums.BEC;
import com.hulk.dryad.common.exception.BaseRteException;
import com.hulk.dryad.common.util.SecurityUtils;
import com.hulk.dryad.persistence.entity.WorkflowLeave;
import com.hulk.dryad.activiti7.service.IWorkflowLeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 *
 * @author hulk
 * @date 2020-10-28
 */
@Api(value = "task", tags = "请假工作流")
@AllArgsConstructor
@RestController
@RequestMapping("/workflow/leave")
public class WorkflowLeaveController{

    private IWorkflowLeaveService workflowLeaveService;

    private WorkflowLeaveStruct workflowLeaveStruct;

    /**
     * 查询请假列表
     */
	@ApiOperation(value = "getPageLeave", notes = "查询请假列表")
	@PrintlnLog
    @GetMapping("/page")
    public R<Page> getPageLeave( Page page ,WorkflowLeaveVO vo) {

		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
        workflowLeave.setCreatedName(SecurityUtils.getUser().getUsername());
		List<WorkflowLeave> list = workflowLeaveService.selectWorkflowLeaveAndTaskNameList(page, workflowLeave);
		List<WorkflowLeaveVO>  vos = workflowLeaveStruct.from(list);
		page.setRecords(vos);
        return R.ok(page);
    }
    /**
     * 查询部门请假列表
     */
	@ApiOperation(value = "getPageLeaveDept", notes = "查询部门请假列表")
	@PrintlnLog
    @GetMapping("/page/dept")
    public R getPageLeaveDept(Page page ,WorkflowLeaveVO vo) {
		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
		Page<WorkflowLeave> rpm = workflowLeaveService.pageWorkflowLeave(page,workflowLeave);
		Page<WorkflowLeaveVO> rpv =	workflowLeaveStruct.from(rpm);
		return R.ok(rpv);
    }

    /**
     * 导出请假列表
     */
	@ApiOperation(value = "export", notes = "导出请假列表")
	@RespExcel(name = "请假列表", sheet = "sheet1")
	@PreAuthorize("@pms.hasPermission('workflow_leave_export')")
	@GetMapping("/export")
	public List<WorkflowLeaveVO> export(Page page, WorkflowLeaveVO vo) {
		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
		List<WorkflowLeave>  modelList = workflowLeaveService.selectWorkflowLeaveList(workflowLeave);
		List<WorkflowLeaveVO>  voList = workflowLeaveStruct.from(modelList);
		if (voList.isEmpty()) {
			throw new BaseRteException(BEC.E_2025);
		}
		return voList;
	}


    /**
     * 获取请假详细信息
     */
	@ApiOperation(value = "getInfo", notes = "获取请假详细信息通过ID")
	@PrintlnLog
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") String id) {
		WorkflowLeave  workflowLeave =workflowLeaveService.selectWorkflowLeaveById(id);
		WorkflowLeaveVO vo=	workflowLeaveStruct.from(workflowLeave);
		return R.ok(vo);
    }

    /**
     * 获取请假详细信息
     */
	@ApiOperation(value = "getInfoByInstanceId", notes = "获取请假详细信息通过实例ID")
	@PrintlnLog
    @GetMapping(value = "/info/{instanceId}")
    public R getInfoByInstanceId(@PathVariable("instanceId") String instanceId) {
		WorkflowLeave  workflowLeave = workflowLeaveService.selectWorkflowLeaveByInstanceId(instanceId);
		WorkflowLeaveVO vo=	workflowLeaveStruct.from(workflowLeave);
        return R.ok(vo);
    }

    /**
     * 新增请假
     */
	@ApiOperation(value = "saveLeave", notes = "新增请假")
	@OperLog(value ="新增请假",action = ActionType.ADD)
	@Idempotent
    @PostMapping
	@PreAuthorize("@pms.hasPermission('workflow_leave_add')")
    public R save(@RequestBody WorkflowLeaveVO vo) {
		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
		int rows = (workflowLeaveService.insertWorkflowLeave(workflowLeave));
		return rows > 0 ? R.ok() : R.failed();
    }

    /**
     * 修改请假
     */
	@ApiOperation(value = "updateLeave", notes = "修改请假")
	@OperLog(value ="修改请假",action = ActionType.UPDATE)
	@Idempotent
    @PutMapping
	@PreAuthorize("@pms.hasPermission('workflow_leave_edit')")
    public R update(@RequestBody WorkflowLeaveVO vo) {
		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
	    int rows =	workflowLeaveService.insertWorkflowLeave(workflowLeave);
        return rows > 0 ? R.ok() : R.failed();
    }

    /**
     * 删除请假
     */
	@ApiOperation(value = "removeLeave", notes = "删除请假")
	@OperLog(value ="删除请假",action = ActionType.DEL)
	@Idempotent
    @DeleteMapping("/{ids}")
	@PreAuthorize("@pms.hasPermission('workflow_leave_del')")
    public R remove(@PathVariable String[] ids) {
		int rows =	workflowLeaveService.deleteWorkflowLeaveByIds(ids);
        return  rows > 0 ? R.ok() : R.failed();
    }
}
